﻿/************************************************************************

 This file is part of "10 Second Tim".

 "10 Second Tim" is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 "10 Second Tim" is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with "10 Second Tim".  If not, see <http://www.gnu.org/licenses/>.

************************************************************************/

// All code (c)2013 Scott Bevin. all rights reserved

using System.Collections.Generic;

namespace LDE.Input
{
	public class InputManager
	{
		public static float TapTime = 0.4f;
		public static float Holdtime = 0.2f;

		public void Initialise( )
		{
			LookForGamepads( );
			LookForKeyboards( );
			LookForMice( );
		}

		public void Update( float dt )
		{
			for ( int i = 0; i < m_inputDevices.Count; ++i )
				m_inputDevices[ i ].UpdateState( dt );
		}

		public IInputDevice FindDeviceTappingInput( InputTrigger trigger )
		{
			foreach ( IInputDevice device in m_inputDevices )
			{
				if ( device.IsTapped( trigger ) != null )
					return device;
			}

			return null;
		}

		public IInputDevice FindDevice( int deviceTypeId, int deviceId )
		{
			foreach ( IInputDevice device in m_inputDevices )
				if ( device.GetDeviceTypeId( ) == deviceTypeId && device.GetDeviceId( ) == deviceId )
					return device;

			return null;
		}

		#region Private Methods

		private void LookForGamepads( )
		{
#if SILVERLIGHT ||  XBOX || WINDOWS
			const int MaxGamePads = 4;

			for ( int i = 0; i < MaxGamePads; ++i )
			{
				m_inputDevices.Add( new GamePadInputDevice( i ) );
			}
#endif
		}

		private void LookForKeyboards( )
		{
#if SILVERLIGHT
			const int MaxKeyboards = 1;
#else
			const int MaxKeyboards = 4;
#endif
			for ( int i = 0; i < MaxKeyboards; ++i )
			{
				m_inputDevices.Add( new KeyboardInputDevice( i ) );
			}
		}

		private void LookForMice( )
		{
#if WINDOWS || SILVERLIGHT
			m_inputDevices.Add( new MouseInputDevice( ) );
#endif
		}

		#endregion Private Methods

		#region Members

		private List<IInputDevice> m_inputDevices = new List<IInputDevice>( );

		#endregion Members
	}
}